package com.sam.chat.service.impl;

import com.sam.chat.entity.ChatGroupDao;

import com.sam.chat.entity.ChatGroupMemberDao;
import com.sam.chat.mapper.ChatGroupMapper;

import com.sam.chat.mapper.ChatGroupMemberMapper;
import com.sam.chat.mapper.ChatUserMapper;
import com.sam.chat.service.IChatGroupService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sam.chat.utils.RedisKeyUtils;
import com.sam.chat.utils.RedisUtils;
import com.sam.chat.vo.GroupHeaderVo;
import com.sam.chat.vo.GroupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 群
 * </p>
 *
 * @author sam
 * @since 2023-01-31
 */
@Service
public class ChatGroupServiceImpl extends ServiceImpl<ChatGroupMapper, ChatGroupDao> implements IChatGroupService {

    @Autowired
    private ChatGroupMapper chatGroupMapper;

    @Autowired
    private ChatGroupMemberMapper chatGroupMemberMapper;

    @Autowired
    private ChatUserMapper chatUserMapper;

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public boolean createGroup(String groupName, String userId, ArrayList<String> memberIdList) {

        String groupId = UUID.randomUUID().toString();
        ChatGroupDao chatGroupDao = new ChatGroupDao();
        chatGroupDao.setGroupId(groupId);
        chatGroupDao.setGroupName(groupName);
        chatGroupDao.setCreateTime(LocalDateTime.now());
        chatGroupDao.setType(0);
        int result1 = chatGroupMapper.insert(chatGroupDao);

        for (String memberId : memberIdList) {
            ChatGroupMemberDao chatGroupMemberDao = new ChatGroupMemberDao();
            chatGroupMemberDao.setGroupId(groupId);
            chatGroupMemberDao.setMemberId(Integer.parseInt(memberId));
            //普通群成员
            chatGroupMemberDao.setType(0);
            chatGroupMemberDao.setCreateTime(LocalDateTime.now());
            chatGroupMemberDao.setUpdateTime(LocalDateTime.now());
            chatGroupMemberMapper.insert(chatGroupMemberDao);
        }

        ChatGroupMemberDao chatDao2 = new ChatGroupMemberDao();
        chatDao2.setGroupId(groupId);
        chatDao2.setMemberId(Integer.parseInt(userId));
        //普通群成员
        chatDao2.setType(1);
        chatDao2.setCreateTime(LocalDateTime.now());
        chatDao2.setUpdateTime(LocalDateTime.now());
        chatGroupMemberMapper.insert(chatDao2);
        return result1 == 1;
    }

    @Override
    public boolean deleteGroup(String groupId) {
        ArrayList<Integer> memberList = chatGroupMemberMapper.findMemberList(groupId);
        for (Integer memberId : memberList) {
            //删除首页的那条消息
            redisUtils.deleteHash(RedisKeyUtils.getRedisGroupRoomListKey(memberId.toString()), groupId);
            //删除未读消息
            redisUtils.del(RedisKeyUtils.getRedisUnReadKey(groupId, memberId.toString()));
        }
        int result = chatGroupMapper.deleteGroup(groupId);
        int result1 = chatGroupMemberMapper.deleteMember(groupId);
        return result > 0 && result1 > 0;
    }

    @Override
    public boolean exitGroup(String groupId, String userId) {
        int result = chatGroupMemberMapper.deleteUserById(groupId,userId);
        return result == 1;
    }

    @Override
    public List<GroupVo> getGroupList(String userId) {

        List<GroupVo> groupList = chatGroupMapper.getGroupList(userId);
        //加入头像及id
        if (groupList != null && !groupList.isEmpty()) {
            for (GroupVo groupVo : groupList) {
                ArrayList<Integer> memberList = chatGroupMemberMapper.findMemberList(groupVo.getGroupId());
                ArrayList<GroupHeaderVo> headerList = new ArrayList<>();
                for (Integer memberId : memberList) {
                    if (headerList.size() < 8) {
                        String header = chatUserMapper.findHeader(memberId);
                        headerList.add(new GroupHeaderVo(memberId.toString(), header));
                    }
                }
                groupVo.setHeaders(headerList);
                groupVo.setMemberIds(memberList);
            }
        }
        return groupList;
    }

    @Override
    public GroupVo getGroupInfo(String groupId) {
        GroupVo groupVo = chatGroupMapper.getGroupInfo(groupId);
        if (groupVo == null) {
            return null;
        }
        ArrayList<Integer> memberList = chatGroupMemberMapper.findMemberList(groupId);
        ArrayList<GroupHeaderVo> headerList = new ArrayList<>();
        for (Integer memberId : memberList) {
            if (headerList.size() < 8) {
                String header = chatUserMapper.findHeader(memberId);
                headerList.add(new GroupHeaderVo(memberId.toString(), header));
            }
        }
        groupVo.setHeaders(headerList);
        groupVo.setMemberIds(memberList);
        return groupVo;
    }

    @Override
    public boolean modifyGroupName(String groupId, String name) {
        int result = chatGroupMapper.modifyGroupName(groupId, name);
        return result ==1;
    }

    @Override
    public boolean addGroupMembers(String groupId, ArrayList<String> memberIdList) {
        for (String memberId : memberIdList) {
            ChatGroupMemberDao chatGroupMemberDao = new ChatGroupMemberDao();
            chatGroupMemberDao.setGroupId(groupId);
            chatGroupMemberDao.setMemberId(Integer.parseInt(memberId));
            //普通群成员
            chatGroupMemberDao.setType(0);
            chatGroupMemberDao.setCreateTime(LocalDateTime.now());
            chatGroupMemberDao.setUpdateTime(LocalDateTime.now());
            chatGroupMemberMapper.insert(chatGroupMemberDao);
        }
        return true;
    }
}
